﻿#include <gst/gst.h>

int
tutorial_main_1(int argc, char* argv[])
{
    GstElement* pipeline;
    GstBus* bus;
    GstMessage* msg;

    /* 初始化GStreamer */
    gst_init(&argc, &argv);

    /* 构建管道 */
    pipeline =
        gst_parse_launch
        ("playbin uri=rtsp://192.168.1.123:554",
            NULL);

    /* 进入播放状态 */
    gst_element_set_state(pipeline, GST_STATE_PLAYING);

    /* 获取元素总线 */
    bus = gst_element_get_bus(pipeline);
    /* 阻塞等待直到错误/视频结束 */
    msg =
        gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
            GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

    /* 若接收到错误消息 */
    if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR) {
        g_printerr("An error occurred! Re-run with the GST_DEBUG=*:WARN "
            "environment variable set for more details.\n");
    }

    /* 释放资源 */
    gst_message_unref(msg);
    gst_object_unref(bus);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    return 0;
}
